package cn.iocoder.yudao.module.system.controller.admin.userChangeMobileRecordrecord;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.userChangeMobileRecordrecord.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userChangeMobileRecordrecord.UserChangeMobileRecordrecordDO;
import cn.iocoder.yudao.module.system.convert.userChangeMobileRecordrecord.UserChangeMobileRecordrecordConvert;
import cn.iocoder.yudao.module.system.service.userChangeMobileRecordrecord.UserChangeMobileRecordrecordService;

@Api(tags = "管理后台 - 企业账号变更")
@RestController
@RequestMapping("/system/user-change-mobile-recordrecord")
@Validated
public class UserChangeMobileRecordrecordController {

    @Resource
    private UserChangeMobileRecordrecordService userChangeMobileRecordrecordService;
    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @ApiOperation("创建用户手机更新记录")
    public CommonResult<Long> createUserChangeMobileRecordrecord(@RequestBody UserChangeMobileRecordrecordCreateReqVO createReqVO) {
        AdminUserRespDTO adminUserRespDTO=adminUserApi.getUser(createReqVO.getOldMobile());
        createReqVO.setNickname(adminUserRespDTO.getNickname());
        createReqVO.setUsername(createReqVO.getOldMobile());
        createReqVO.setStatus(0);
        createReqVO.setUserId(adminUserRespDTO.getId());
        return success(userChangeMobileRecordrecordService.createUserChangeMobileRecordrecord(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户手机更新记录")
    public CommonResult<Boolean> updateUserChangeMobileRecordrecord(@Valid @RequestBody UserChangeMobileRecordrecordUpdateReqVO updateReqVO) {
        userChangeMobileRecordrecordService.updateUserChangeMobileRecordrecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户手机更新记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteUserChangeMobileRecordrecord(@RequestParam("id") Long id) {
        userChangeMobileRecordrecordService.deleteUserChangeMobileRecordrecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户手机更新记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<UserChangeMobileRecordrecordRespVO> getUserChangeMobileRecordrecord(@RequestParam("id") Long id) {
        UserChangeMobileRecordrecordDO userChangeMobileRecordrecord = userChangeMobileRecordrecordService.getUserChangeMobileRecordrecord(id);
        return success(UserChangeMobileRecordrecordConvert.INSTANCE.convert(userChangeMobileRecordrecord));
    }

    @PostMapping("/list")
    @ApiOperation("获得用户手机更新记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<UserChangeMobileRecordrecordRespVO>> getUserChangeMobileRecordrecordList(@RequestBody @RequestParam("ids") Collection<Long> ids) {
        List<UserChangeMobileRecordrecordDO> list = userChangeMobileRecordrecordService.getUserChangeMobileRecordrecordList(ids);
        return success(UserChangeMobileRecordrecordConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("企业账号变更审核列表")
    @TenantIgnore
    public CommonResult<PageResult<UserChangeMobileRecordrecordRespVO>> getUserChangeMobileRecordrecordPage(@Valid @RequestBody UserChangeMobileRecordrecordPageReqVO pageVO) {
        PageResult<UserChangeMobileRecordrecordDO> pageResult = userChangeMobileRecordrecordService.getUserChangeMobileRecordrecordPage(pageVO);
        return success(UserChangeMobileRecordrecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户手机更新记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-change-mobile-recordrecord:export')")
    @OperateLog(type = EXPORT)
    public void exportUserChangeMobileRecordrecordExcel(@Valid UserChangeMobileRecordrecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserChangeMobileRecordrecordDO> list = userChangeMobileRecordrecordService.getUserChangeMobileRecordrecordList(exportReqVO);
        // 导出 Excel
        List<UserChangeMobileRecordrecordExcelVO> datas = UserChangeMobileRecordrecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户手机更新记录.xls", "数据", UserChangeMobileRecordrecordExcelVO.class, datas);
    }


    @PutMapping("/authenticationUserPhone")
    @ApiOperation("认证用户手机更新")
    @TenantIgnore
    public CommonResult<Boolean> authenticationUserPhone (@RequestBody UserChangeMobileRecordrecordUpdateReqVO updateReqVO) {
        if(null==updateReqVO.getStatus() && null!=updateReqVO.getStatuss()){
            updateReqVO.setStatus(updateReqVO.getStatuss());
        }
        userChangeMobileRecordrecordService.authenticationUserPhone(updateReqVO.getId(),updateReqVO.getStatus(),updateReqVO.getRemark());
        return success(true);
    }


}
